package com.bltec.emergency.controller;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.Plan;
import com.bltec.emergency.service.planService.PlanService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import com.bltec.emergency.utils.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/plan")
@RequiresPermissions("project:plan")
public class PlanController {
    @Resource
    private PlanService planService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllPlan")
    public ResponseJson getAllPlan() {
        try {
            List<Plan> plan = planService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(plan));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*private String name;
    private String recordInfo;
    private Integer planLevel;
    private String planType;
    private String fileUrl;*/
    @RequiresPermissions("plan:insert")
    @RequestMapping("addPlan")
    public ResponseJson addPlan(String name, String recordInfo, Integer planLevel, String planType,@RequestParam(value = "fileUrl",required = false) MultipartFile fileUrl) {
        try {
            String filePath=FileUtils.writeToFile(fileUrl.getBytes(), ResourceUtils.getURL("classpath:").getPath(),fileUrl.getOriginalFilename());
            Plan plan = new Plan();
            plan.setName(name);
            plan.setRecordInfo(recordInfo);
            plan.setPlanLevel(planLevel);
            plan.setPlanType(planType);
            plan.setFileUrl(filePath);
            plan.setEnable(true);
            plan.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            plan.setCreateTime(DateUtil.dateNow());
            planService.add(plan);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    @RequiresPermissions("plan:update")
    @RequestMapping("updatePlan")
    public ResponseJson updatePlan(Long id,String name, String recordInfo, Integer planLevel, String planType,@RequestParam(value = "fileUrl",required = false) MultipartFile fileUrl) {
        try {
            String filePath=FileUtils.writeToFile(fileUrl.getBytes(), ResourceUtils.getURL("classpath:").getPath(),fileUrl.getOriginalFilename());
            Plan plan = planService.getPlanById(id);
            plan.setName(name);
            plan.setRecordInfo(recordInfo);
            plan.setPlanLevel(planLevel);
            plan.setPlanType(planType);
            plan.setFileUrl(filePath);
            plan.setEnable(true);
            plan.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            plan.setUpdateTime(DateUtil.dateNow());
            planService.updatePlanById(plan);
            return new ResponseJson(Constant.OK, JSON.toJSON(plan));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }
    @RequiresPermissions("plan:delete")
    @RequestMapping("deletePlan")
    public ResponseJson setDisable(Long id) {
        if (null != planService.getPlanById(id)) {
            if (planService.deletePlanById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
